<template>
  <table class="input-radio">
    <tbody>
      <tr v-for="(line,index) of lineList" :key="index">
        <td v-for="(radio,index) of line" :key="radio.key" class="radio-item">
          <div class="radio-text" @click="onClick(radio)">
            <span class="radio-label" :class="{checked:(value == radio.key)}"></span>
            <span class="radio-key">{{radio.text||radio.key}}</span>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  name: 'inputRadio',
  props: ['config', 'value'],
  computed: {
    lineList() {
      let tr = [[]];
      this.config.extra.radio.forEach((item, index) => {
        tr[tr.length - 1].push(item);
        if (tr[tr.length - 1].length >= this.columnsLen) {
          tr.push([]);
        }
      });
      return tr;
    }
  },
  data() {
    return {
      columnsLen: 5
    };
  },
  methods: {
    onClick(radio) {
      // 第三个参数true 表示本组件的值与config.value同步，对于这种状态只能从外部修改的自定义组件，需要采取这种方式
      this.$emit('input', {value: radio.key, twoWay: true});
    }
  },
  created(){
    if (this.value) {
      this.$emit('input', {value: this.value, twoWay: true});
    }
  }
};
</script>

<style lang="less">
.input-item {
  input[type=text], textarea, select {
    display: block;
    box-sizing: border-box;
    width: 100%;
    min-height: 36px;
    padding: 0 1em;
    border: 1px solid #e0e0e0;
    background: transparent;
    font-size: 12px;
    font-family: "Arial", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica", "sans-serif";
    line-height: 34px;
    outline: none;
    transition: .5s;
    &:focus {
      border-color: #2e9057;
      transition: .25s;
    }
  }
  textarea {
    min-height: 6em;
    padding: .5em 1em;
    line-height: 1.5em;
  }
  select {
    padding-right: 0;
  }

  &.error {
    input[type=text], textarea, select {
      border-color: #ff6152;
    }
  }
}

.input-cover {
  .preview-img {
    position: relative;
    float: left;
    width: 130px;
    height: 86px;
    margin-right: 15px;
    border: 1px solid #e0e0e0;
    background: url("../../../assets/upload_cover_bg.png") center no-repeat;
    transition: .5s;
    &.has-img {
      background-size: contain;
    }
    &:after{
      content: '上传中';
      display: block;
      position: absolute;
      width: 100%;
      top: 50%;
      left: 0;
      margin-top: -.5em;
      color: #fff;
      text-align: center;
      line-height: 1em;
      opacity: 0;
      visibility: hidden;
      transition: .5s;
    }
    &:before{
      content: ' ';
      display: block;
      height: 100%;
      width: 100%;
      top: 0;
      left: 0;
      background-color: rgba(0,0,0,0.25);
      opacity: 0;
      visibility: hidden;
      transition: .5s;
    }
    &.uploading{
      &:before,&:after{
        visibility: visible;
        opacity: 1;
      }
    }
  }
  .upload-btn {
    float: left;
    position: relative;
    width: 54px;
    margin-top: 20px;
    border-radius: 5px;
    background: #2e9057;
    color: #fff;
    font-family:simsun, serif;
    font-size: 14px;
    text-align: center;
    line-height: 27px;
    transition: .25s;
    &:hover{
      background: #4da672;
    }
    &:active{
      transition: .1s;
      background: #2e9057;
    }
  }
  .upload-info {
    padding-top: 50px;
    color: #999;
    font-family:simsun, serif;
    font-size: 12px;
  }
  .hidden-file-input {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}

.input-static-text {
  font-size: 20px;
  color: #999;
}

.input-editable-text {
  position: relative;
  input {
    padding-right: 60px;
    &[readonly], &[readonly]:focus {
      padding-left: 0;
      border-color: transparent;
      font-size: 20px;
      color: #999;
      cursor: text;
    }
  }
  .edit-btn {
    position: absolute;
    right: 1px;
    top: 1px;
    background: url('../../../assets/article_alt.png') no-repeat 8px center;
    color: #4a9bac;
    font-size: 12px;
    line-height: 34px;
    cursor: pointer;
    transition: .25s;
    &:hover {
      background-color: #f0f0f0;
    }
    padding-left: 28px;
    padding-right: 8px;
  }
}

.input-radio {
  user-select: none;
  .radio-label {
    box-sizing: border-box;
    transition: all .3s;
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 5px;
    padding: 1px;
    border: 1px #e0e0e0 solid;
    border-radius: 6px;
    vertical-align: middle;
    &:after{
      content:'';
      display: block;
      width: 100%;
      height: 100%;
      background: #2e9057;
      opacity: 0;
      transition: all .3s;
      transform: scale(.75);
      border-radius: 6px;
    }
    &.checked {
      //background: #2e9057;
      border-color: #2e9057;
      &:after{
        opacity: 1;
        transform: scale(1);

      }
    }
  }
  .radio-key {
    color: #a1a1a1;
  }
  .radio-item {
    vertical-align: middle;
  }
  .radio-text {
    display: inline-block;
    margin-right: 16px;
    white-space: nowrap;
    cursor: pointer;
  } 
}

.input-checkbox {
  user-select: none;
  .checkbox-label {
    box-sizing: border-box;
    transition: all .3s;
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 5px;
    padding: 1px;
    border: 1px #e0e0e0 solid;
    vertical-align: middle;
    &:after{
      content:'';
      display: block;
      width: 100%;
      height: 100%;
      background: #2e9057;
      opacity: 0;
      transition: all .3s;
      transform: scale(.75);
    }
    &.checked {
      //background: #2e9057;
      border-color: #2e9057;
      &:after{
        opacity: 1;
        transform: scale(1);

      }
    }
  }
  .checkbox-key {
    color: #a1a1a1;
  }
  .checkbox-item {
    vertical-align: middle;
  }
  .checkbox-text {
    display: inline-block;
    margin-right: 16px;
    white-space: nowrap;
    cursor: pointer;
  } 
}

.input-agree {
  .agree-icon {
    display: inline-block;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    vertical-align: middle;
    cursor: pointer;
    transition: all .3s;
    border: 1px solid #82c553;
    &.checked {
      background: url("../../../assets/agree_checked.png") center no-repeat;
    }
  }
  .agree-text {
    display: inline-block;
    color: #999;
    vertical-align: middle;
  }
  .agree-words {
    display: inline-block;
    margin: 0 6px;
  }
}

.input-desc-link {
  color: #4a9bac;
  &:hover {
    color: #4a9bbc;
  }
}
</style>